package com.Algorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.Entity.Employee;

/**
 *
 * @author Jadzia
 */
public class PreferenceMatrix {
    
	public List<double[][]> allPreferenceMatrix = new ArrayList<double[][]>();

	private int numberOfEmployees;
	private int numberOfCriterion = Criterion.NUMBER_OF_CRITERIONS;
	private double[] sattyScale = new double[9];
	private List<Employee> employees;
	private List<List<Double>> criterionOfEmployees;
	
	public PreferenceMatrix(List<Employee> employees) 
	{
		this.numberOfEmployees = employees.size();
		this.employees = employees;
		criterionOfEmployees = new ArrayList<List<Double>>();
		infoAboutEmployees(); 
		createAllPreferenceMatrix();
	}
	
	private void infoAboutEmployees() {
		for (int i = 0; i < employees.size(); i++) 
		{
			Criterion c = new Criterion(employees.get(i));
			criterionOfEmployees.add(Arrays.asList(c.criterion_1(), c.criterion_2(), c.criterion_3(), 
												   c.criterion_4(), c.criterion_5(), c.criterion_6()));
		}
	}

	/*
	private List<List<Double>> criterionOfEmployees = new ArrayList<List<Double>>() {
		{
                            // k1    k2    k3    k4    k5   k6
            add(Arrays.asList(16.0, 10.5, 0.46, 0.72, 0.5, 17.0));
			add(Arrays.asList(2.0, 7.0, 0.39, 0.43, 1.0, 10.0));
			add(Arrays.asList(0.0, 10.0, 0.71, 0.86, 1.0, 10.0));
			add(Arrays.asList(7.0, 7.5, 0.43, 0.63, 0.17, 19.0));
			add(Arrays.asList(3.0, 9.5, 0.44, 0.33, 0.5, 24.0));
			add(Arrays.asList(0.0, 10.0, 0.76, 0.54, 0.6, 11.0));
		}
	};*/

	private void createAllPreferenceMatrix() 
	{
		for (int i = 0; i < numberOfCriterion; i++) 
		{
			allPreferenceMatrix.add(generateCriterionMatrix(i));
		}
	}

	private double getMaxValue(int pCriterion) 
	{
		double maximum = 0;
		for (int i = 0; i < criterionOfEmployees.size(); i++) 
		{
			if (criterionOfEmployees.get(i).get(pCriterion) > maximum) 
			{
				maximum = criterionOfEmployees.get(i).get(pCriterion);
			}
		}
		return maximum;
	}
    
    private double getMinValue(int pCriterion)
    {
        double minimum = Double.MAX_VALUE;
        for(int i=0; i < criterionOfEmployees.size(); i++)
        {
            if(criterionOfEmployees.get(i).get(pCriterion) < minimum)
            {
                minimum = criterionOfEmployees.get(i).get(pCriterion);
            }
        }
        return minimum;
    }
    
    private double differenceMaxMin(double pMax, double pMin)
    {
        return pMax - pMin;
    }
    
    private void createSattyScale(int pCriteron)
    {
        for(int i = 0; i<sattyScale.length; i++)
        {
            sattyScale[i] = differenceMaxMin(getMaxValue(pCriteron), getMinValue(pCriteron))/sattyScale.length * (i+1);
        }
    }
   
    private double[][] generateCriterionMatrix(int pCriterionNumber)
    {
    	createSattyScale(pCriterionNumber);
    	
    	double[][] criterionMatrix = new double [numberOfEmployees][numberOfEmployees];
    	for(int i = 0; i<criterionOfEmployees.size(); i++)
        {
            for(int j = 0; j<criterionOfEmployees.size(); j++)
            {
                if(i==j)
                {
                	criterionMatrix[i][j] = 1;
                }
                else 
                {
                	double difference = criterionOfEmployees.get(i).get(pCriterionNumber) - criterionOfEmployees.get(j).get(pCriterionNumber);
                	criterionMatrix[i][j] = getSattyScale(difference);
                }
            }
        }
    	
    	return criterionMatrix;
    }
    
	public void printMatrix(double[][] pMatrix) 
	{
		for (int i = 0; i < pMatrix.length; i++) 
		{
			for (int j = 0; j < pMatrix[i].length; j++) 
			{
				System.out.printf("%7.2f", pMatrix[i][j]);
			}
			System.out.print("\n");
		}
	}
          
	private double getSattyScale(double pDifference) 
	{
		if (pDifference > 0) 
		{
			for (int i = 0; i < sattyScale.length; i++) 
			{
				if (pDifference < sattyScale[i]) {
					return i + 1;
				}
			}

			return sattyScale.length;
		} else 
		{
            pDifference *= (-1);
            for (int i = 0; i < sattyScale.length; i++) 
            {
            	if (pDifference < sattyScale[i]) 
            	{
            		return 1.0 / (double)(i + 1);
            	}
            }
            return 1.0 / sattyScale.length;
		}
	}
 
    
}
